package Controller;

import Model.BangDiem;
import Model.KhoaHoc;
import Model.LopHoc;
import Model.MonHoc;
import Model.SinhVien;
import Model.Users;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
*
* @author Nhóm java lớp PNV2T
* 				- Nguyễn Thị Lệ Đài
* 				- Nguyễn Chí Hướng
* 
*	Contain all update function of application
*/
public class Update {
    private ConnectDB con = null;
    private ResultSet rs = null;
    /** Creates a new instance of Update */
    public Update() {
        try {
            con = new ConnectDB();
            con.open();
        } catch (Exception ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public int SuaLopHoc(LopHoc newLop){
        String sql = "Update LopHoc Set TenLop = ?, KhoaHoc=?, HeDaoTao = ?, ChuNhiem = ?, LopTruong=? Where ID=?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newLop.getTenLop());
            con.setInt(2,newLop.getMaKhoa());
            con.setString(3,newLop.getHeDaoTao());
            con.setString(4,newLop.getChuNhiem());
            con.setString(5,newLop.getLopTruong());
            con.setInt(6,newLop.getID());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public int SuaKhoaHoc(KhoaHoc newKH){
        String sql = "Update KhoaHoc Set TenKhoa = ?, BatDau=? Where ID=?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newKH.getTenKhoa());
            con.setString(2,newKH.getBatDau());
            con.setInt(3,newKH.getID());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public int SuaSinhVien(SinhVien SV){
        String sql = "Update SinhVien Set Ten = ?, QueQuan=?,NgaySinh=?,GioiTinh=?,TonGiao=? Where ID=?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,SV.getTen());
            con.setString(2,SV.getQueQuan());
            con.setString(3,SV.getNgaySinh());
            con.setString(4,SV.getGioiTinh());
            con.setString(5,SV.getTonGiao());
            con.setInt(6,SV.getID());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public int SuaBangDiem(BangDiem diem){
        String sql = "Update KetQuaHocTap Set DiemkiemTra = ?, DiemChuyenCan=?, Diemthi=?, DiemTongKet=?  Where MaSV=? and MaMon=?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setDouble(1,diem.getDiemKiemTra());
            con.setDouble(2,diem.getDiemChuyenCan());
            con.setDouble(3,diem.getDiemThi());
            con.setDouble(4,diem.getDiemTongKet());
            con.setInt(5,diem.getMaSV());
            con.setInt(6,diem.getMaMon());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public int SuaMonHoc(MonHoc mon){
        String sql = "Update MonHoc Set TenMon = ?, KyHoc=?, NamHoc=?, SoTinChi=?  Where ID=?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,mon.getTenMon());
            con.setInt(2,mon.getKyHoc());
            con.setInt(3,mon.getNamHoc());
            con.setInt(4,mon.getSoTinChi());
            con.setInt(5,mon.getID());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }
    
    public void CloseData(){
        try {
            con.close();
        } catch (Exception ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

	public int SuaTaiKhoan(Users newTaiKhoan) {
        String sql = "Update Users Set TaiKhoan = ?, MatKhau = ?, TenXacNhan = ? Where ID = ?";
        con.setMode(ConnectDB.MODE_PREPARED);
        try {
            con.setStatement(sql);
            con.setString(1,newTaiKhoan.getTaiKhoan());
            con.setString(2,newTaiKhoan.getMatKhau());
            con.setString(3,newTaiKhoan.getTenXacNhan());
            con.setInt(4,newTaiKhoan.getId());
            return con.doUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            ex.printStackTrace();
        }
        return 0;
    }
    
}
